package com.meritdata.cloud.data.standard.table.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.meritdata.cloud.data.base.ResultData;
import com.meritdata.cloud.data.standard.table.service.IDictPartStructService;
import com.meritdata.cloud.utils.SessionUtils;

@RestController
@RequestMapping("/dict/part")
public class DictPartStructController {

	@Autowired
	private IDictPartStructService dictPartStructService;

	@Autowired
	private SessionUtils sessionUtils;

	@RequestMapping("/list")
	public ResultData list(HttpServletRequest request, @RequestParam Long dictId) {
		ResultData result = new ResultData();
		result.setData(dictPartStructService.list(dictId));
		return result;
	}

	@RequestMapping("/delete")
	public ResultData delete(HttpServletRequest request, @RequestParam long id) {
		return dictPartStructService.delete(id);
	}

	@SuppressWarnings("unchecked")
	@RequestMapping("/modify")
	public ResultData modify(HttpServletRequest request, @RequestParam String parts) {
		List<Map<String, Object>> partObjs = (List<Map<String, Object>>) JSON.parseObject(parts, List.class);
		return dictPartStructService.modify(partObjs, sessionUtils.getCurrentUserId());
	}

	@RequestMapping("/create")
	public ResultData create(HttpServletRequest request, @RequestParam String name, @RequestParam String code,
			@RequestParam Long dictId) {
		return dictPartStructService.create(name, code, dictId, sessionUtils.getCurrentUserId());
	}

	@RequestMapping("/update")
	public ResultData update(HttpServletRequest request, @RequestParam String name, @RequestParam String code,
			@RequestParam long id) {
		return dictPartStructService.update(name, code, id, sessionUtils.getCurrentUserId());
	}

}
